package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Asignaturaxcomponente;


public class AsignaturaxcomponenteDAO implements IAsignaturaxcomponenteDAO {
    private static final Log log = LogFactory.getLog(AsignaturaxcomponenteDAO.class);

    // property constants
    //public static final Integer  ASIGXCOMPCIAIDN = "asigxcompciaidn";
    public static final String ASIGXCOMPCIAIDN = "asigxcompciaidn";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Asignaturaxcomponente Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Asignaturaxcomponente instance) {
        log.debug("saving Asignaturaxcomponente instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Asignaturaxcomponente Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Asignaturaxcomponente instance) {
        log.debug("deleting Asignaturaxcomponente instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Asignaturaxcomponente Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Asignaturaxcomponente instance) {
        log.debug("updating Asignaturaxcomponente instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Asignaturaxcomponente findById(Integer id) {
        log.debug("finding Asignaturaxcomponente instance with id: " + id);

        try {
            Asignaturaxcomponente instance = (Asignaturaxcomponente) getSession()
                                                                         .get(Asignaturaxcomponente.class,
                    id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Asignaturaxcomponente failed", re);
            throw re;
        }
    }

    public List<Asignaturaxcomponente> findByExample(
        Asignaturaxcomponente instance) {
        log.debug("finding Asignaturaxcomponente instance by example");

        try {
            List results = getSession().createCriteria("Asignaturaxcomponente")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Asignaturaxcomponente entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Asignaturaxcomponente> found by query
        */
    public List<Asignaturaxcomponente> findByProperty(String propertyName,
        Object value) {
        log.debug("finding Asignaturaxcomponente instance with property: " +
            propertyName + ", value: " + value);

        try {
            String queryString = "from Asignaturaxcomponente as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Asignaturaxcomponente> findByAsigxcompciaidn(
        Object asigxcompciaidn) {
        return findByProperty(ASIGXCOMPCIAIDN, asigxcompciaidn);
    }

    /**
    * Find all Asignaturaxcomponente entities.
    *
    * @return List<Asignaturaxcomponente> all Asignaturaxcomponente instances
    */
    public List<Asignaturaxcomponente> findAll() {
        log.debug("finding all Asignaturaxcomponente instances");

        try {
            String queryString = "from Asignaturaxcomponente";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Asignaturaxcomponente> findByCriteria(String whereCondition) {
        log.debug("finding Asignaturaxcomponente " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Asignaturaxcomponente model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Asignaturaxcomponente> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Asignaturaxcomponente failed", re);
            throw re;
        }
    }

    public List<Asignaturaxcomponente> findPageAsignaturaxcomponente(
        String sortColumnName, boolean sortAscending, int startRow,
        int maxResults) {
        log.debug("finding Asignaturaxcomponente findPageAsignaturaxcomponente");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Asignaturaxcomponente model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Asignaturaxcomponente model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberAsignaturaxcomponente() {
        log.debug("finding Asignaturaxcomponente count");

        try {
            String queryString = "select count(*) from Asignaturaxcomponente model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
